<template>
  <el-form-item v-if="showSalesman" :label="title" prop="salesmanId" :rules="rules">
    <el-select v-model="selected" filterable :placeholder="placeholder" @change="selectChange">
      <el-option
        v-for="(item,index) in list"
        :key="index"
        :label="item.val"
        :value="item.code"
      />
    </el-select>
    <!-- <form-remote-select
      v-model="selected"
      :url="salesmanUrl"
      :disabled="disabled"
      :placeholder="placeholder"
      @change="change"
    /> -->
  </el-form-item>
</template>
<script>
import adminSupport from 'lola-admin-support-el'

export default {
  name: 'DutyUsers',
  props: {
    mode: {
      type: String,
      default: 'form'
    },
    value: {// 设置value为props属性-必须
      type: [String],
      default: ''
    },
    dutyId: {
      type: [String],
      default: null
    },
    lable: {
      type: [String],
      default: '员工'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  data() {
    return {
      salesmanUrl: '',
      showSalesman: true,
      rules: { required: true, message: '必填', trigger: 'blur' },
      title: null,
      list: []
    }
  },
  computed: {
    selected: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  created() {
    // 是否启动验证
    if (this.mode === 'form') {
      this.rules = adminSupport.lib.ruleStore.def
      this.title = this.lable
    } else {
      this.title = null
    }
    var that = this
    this.salesmanUrl = adminSupport.lib.service.conf.api.get('portal').url + '/api/user/findAll/duty/' + this.dutyId + '?key='
    this.$http.get(this.salesmanUrl).then(function(rsp) {
      that.loading = false
      if (rsp.code !== 200) {
        that.$message.error(rsp.message)
        return
      } else {
        if (rsp.data.length > 0) {
          const _data = Object.keys(rsp.data[0])
          that.code = _data[0]
          that.val = _data[1]
        }
        that.list = rsp.data
      }
    }).catch(function(err) {
      console.log(err)
    })
  },
  methods: {
    selectChange(current) {
      const cur = this.list.find(item => { return item.code === current })
      this.$emit('change', cur)
    }
  }
}
</script>
